# -*- coding: utf-8 -*-
'''
@文件    :department.py
@说明    :部门管理
@时间    :2023/12/19 19:57:04
@作者    :浪晋
@版本    :1.0
'''

from app.admin import auth
from flask import request,g
from utils.requetFunc import RequestParser
from utils.responseFunc import resultMsg
from utils.commomFunc import build_tree,traverse_tree


@auth.route("/department/list",methods=["post"])
def departmentList():
    """
    @功能    :部门列表
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    body = request.get_json()
    department = body.get("department")
    parse = RequestParser()
    parse.add_argument(value=department,name="部门名称",remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "select id,department,parentId,sort,status,remark,createTime from tb_system_department where status != 0 "
    if department:
        sql = sql + " and department like '%{}%'".format(department)
    sql = sql + " order by sort desc,id;"
    result = build_tree(db.fetchall(sql))
    return resultMsg(data=result)


@auth.route("/department/user",methods=["post"])
def department_user():
    """
    @功能    :获取指定的部门的人员列表
    @参数    :
    @返回值  :
    @时间    :2024/01/05 21:22:52
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    sql = "select id,department,parentId from tb_system_department where status = 1;"
    result = build_tree(db.fetchall(sql))
    sql = "select id userId,nickName department,deptId parentId from tb_system_user where  status = 1;"
    userList = db.fetchall(sql)
    userobj = {}
    for user in userList:
        item = userobj.get(user.get("parentId"),[])
        item.append(user)
        userobj[user.get("parentId")] = item
    result = traverse_tree(result,userobj)
    return resultMsg(data=result)

      


@auth.route("/department/create",methods=["post"])
def department_create():
    """
    @功能    :新增部门
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    authorId = g.userInfo.get("id")
    body = request.get_json()
    department = body.get("department")
    parentId = body.get("parentId",0) or 0
    sort = body.get("sort")
    remark = body.get("remark")
    parse = RequestParser()
    parse.add_argument(value=department,name="部门名称",required=True,remark="必填")
    parse.add_argument(value=parentId,name="上级ID",remark="只能是存在的部门id",type=(int))
    parse.add_argument(value=sort,name="排序",required=True,remark="",type=(int))
    parse.add_argument(value=remark,name="备注",remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    body.update(authorId=authorId)
    db.insert(table="tb_system_department",data=body)
    db.commit()
    return resultMsg()
    

@auth.route("/department/edit",methods=["post"])
def department_edit():
    """
    @功能    :修改部门
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    authorId = g.userInfo.get("id")
    body = request.get_json()
    rowId = body.pop("id")
    department = body.get("department")
    parentId = body.get("parentId",0) or 0
    sort = body.get("sort")
    remark = body.get("remark")
    parse = RequestParser()
    parse.add_argument(value=rowId,name="部门ID",required=True,remark="",type=(int))
    parse.add_argument(value=department,name="部门名称",required=True,remark="")
    parse.add_argument(value=parentId,name="上级ID",remark="只能是存在的部门id",type=(int))
    parse.add_argument(value=sort,name="排序",required=True,remark="",type=(int))
    parse.add_argument(value=remark,name="备注",remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    query = {"id":rowId}
    db.update(table="tb_system_department",data=body,query=query)
    db.commit()
    return resultMsg()


@auth.route("/department/del",methods=["post"])
def department_del():
    """
    @功能    :删除部门
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    userId = g.userInfo.get("id")
    body = request.get_json()
    rowId = body.get("id")
    parse = RequestParser()
    parse.add_argument(value=rowId,name="部门ID",required=True,remark="",type=int)
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    query = {"id":rowId}
    db.update(table="tb_system_department",data={"status":0},query=query)
    db.commit()
    return resultMsg()


@auth.route("/department/state",methods=["post"])
def department_state():
    """
    @功能    :设置部门状态
    @参数    :
    @返回值  :
    @时间    :2022/08/30 11:02:54
    @作者    :浪晋
    @版本    :1.0
    """
    db = g.db
    authorId = g.userInfo.get("id")
    body = request.get_json()
    rowId = body.get("id")
    status = body.get("status")
    parse = RequestParser()
    parse.add_argument(value=rowId,name="部门ID",required=True,remark="",type=int)
    parse.add_argument(value=status,name="状态",required=True,type=(int),lenght=(1,2),remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    query = {"id":rowId}
    db.update(table="tb_system_department",data={"status":status,"authorId":authorId},query=query)
    db.commit()
    return resultMsg()